

import MatrixIO
import cern
from Matrix import Matrix
from mfuncs import size
import java,javax,jarray

from javax.swing import JProgressBar,JInternalFrame

from affymetrix.fusion.cdf import *
from affymetrix.fusion.cel import *

try:
	import cern.jet.stat.DoubleDescriptive as Descriptive
except:
	import cern.jet.stat.Descriptive as Descriptive



def summarize(E,Elabels,ExpressionSet,console=None):
	
	genes = len(Elabels)
	[ps,exps] = size(E)	
	X = Matrix(genes,exps)
	gene=0
	for probe in Elabels:
	  for j in range(0,exps):
		IDX = ExpressionSet.PSIDX[probe]
		V = cern.colt.list.DoubleArrayList()
		for i in IDX:
			x=i[0]
			y=i[1]
			idx = ExpressionSet.cel.xyToIndex(x,y)
			#print "call is:", x,y,idx
			V.add(E[idx,j].get(0,0))
			#get all values into a list, get median, add them up, subtract median
		M = Descriptive.median(V)
		S = Descriptive.sum(V)
		X[gene,j] = S-M
	  gene=gene+1	
	return X 


def test(cdf,cel):   # deprecated
	nsets = cdf.getHeader().getNumProbeSets();
	for iset in range(0,nsets):
		sum = 0;
		set = FusionCDFProbeSetInformation();
		cdf.getProbeSetInformation(iset, set);
		ngroups = set.getNumGroups();
		for igroup in range(0,ngroups):
			group = FusionCDFProbeGroupInformation();
			set.getGroup(igroup, group);
			print group.name
			ncells = group.getNumCells();
			for icell in range(0,ncells):
				probe = FusionCDFProbeInformation();
				group.getCell(icell, probe);
				w = cel.xyToIndex(probe.getX,probe.getY)
				print w
				v = cdl.getIntensity(int(probe.getX()), int(probe.getY()))








class ExpressionSet:
	CDF_FILE ='HG_U95Av2.CDF'
	CEL_FILES = []
	PSIDX = {}
	cdf = None
	cel = None
	console = None

	def __init__(self,directory=None,console=None):
		

		self.console = console
		if directory is not None:
			print "Opening directory"
			dir = java.io.File(directory)
			self.CEL_FILES = dir.listFiles()
			print "found directory ", self.CEL_FILES
		else:
			print "Could not find directory"
		
	def processCEL(self):
		# read first cell file, get dimensions
		###debug self.CEL_FILES = self.CEL_FILES[0:3]
		EXPS = len(self.CEL_FILES)
		cel = FusionCELData()
		#print dir(cel)
		self.cel = cel
		M=None
		j=0		
		for f in self.CEL_FILES:
			cel.setFileName(f.getAbsoluteFile().toString())
			cel.read()
			
			N = cel.getCells();
			print N
			if M is None:
				M = Matrix(N,EXPS)
			for x in range(0,N):
				M.set(x,j,cel.getIntensity(x))
			j=j+1
		return M
		
	def readcdf(self):
		self.cdf = FusionCDFData();
		self.cdf.setFileName(self.CDF_FILE);
		self.cdf.read()
		self.cdfheader = self.cdf.getHeader()
		probeset = FusionCDFProbeSetInformation()
		H = self.PSIDX
		for i in range(0,self.cdfheader.getNumProbeSets()):
			name = self.cdf.getProbeSetName(i)
			self.cdf.getProbeSetInformation(i,probeset);
			assert( probeset.getProbeSetType(), FusionGeneChipProbeSetType.ExpressionProbeSetType );
			GROUPS = probeset.getNumGroups();
			group = FusionCDFProbeGroupInformation()
			for g in range(0,GROUPS):
				probeset.getGroup(g, group);
				probe = FusionCDFProbeInformation()
				for y in range(0,probeset.getNumCells()):
					group.getCell(y, probe);
					coord = ()
					Q = (probe.getX(),probe.getY())
					if H.has_key(name):			
							v = H[name]	
							v = v+[Q]
							H[name] = v
					else:
							H[name] = [Q]

